﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using PropertyManagment.config;
using PropertyManagment.entity;
using PropertyManagment.entity.dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PropertyManagment.mapper.impl {
    public class BaseMapperImpl<T> : BaseMapper<T> where T : BaseEntity  {

        public readonly AppDbContext context;
        public DbSet<T> dbSet;

        public BaseMapperImpl(AppDbContext context) {
            this.context = context;
            this.dbSet = context.Set<T>();
        }

        public IEnumerable<T> getAll() {
            return dbSet;
        }

        public T getById(long id) {
            return dbSet.Find(id);
        }

        public IEnumerable<T> selectBatchIds(ICollection<long> idList) {
            //var ids = string.Join(",", idList);
            //防SQL注入正确代码
            //MySqlParameter[] sqlparment = new MySqlParameter[] { new MySqlParameter("@ids", ids) };
            //List<T> list = dbSet.FromSqlRaw(@$"SELECT * FROM sys_menu WHERE FIND_IN_SET(id,@ids);", sqlparment).ToList();

            List<T> list = dbSet.Where<T>(p => idList.Contains(p.id)).ToList();
            return list;
        }

        public T add(T data) {
            dbSet.Add(data);
            context.SaveChanges();
            return data;
        }

        public IEnumerable<T> addBatch(ICollection<T> dataList) {
            foreach (var data in dataList) {
                dbSet.Add(data);
            }
            context.SaveChanges();
            return dataList;
        }

        public T delete(long id) {
            T data = dbSet.Find(id);
            if (data != null) {
                dbSet.Remove(data);
                context.SaveChanges();
            }
            return data;
        }

        public IEnumerable<T> deleteBatch(ICollection<T> dataList) {
            foreach (var data in dataList) {
                dbSet.Remove(data);
            }
            context.SaveChanges();
            return dataList;
        }

        public IEnumerable<T> deleteBatchIds(ICollection<long> idList) {
            var dataList = selectBatchIds(idList);
            foreach (var data in dataList) {
                dbSet.Remove(data);
            }
            context.SaveChanges();
            return dataList;
        }

        public T update(T updateData) {
            var data = dbSet.Attach(updateData);
            data.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            context.SaveChanges();
            return updateData;
        }

        public Page<T> page(Page<T> page) {

            var num = (page.current-1) * page.size;
            if (num >= dbSet.Count()) {
                num = ((int)Math.Ceiling((double)dbSet.Count() / page.size) - 1) * page.size;
            }
            List<T> list = dbSet.Skip(num).Take(page.size).ToList();
            page.records = list;
            page.total = list.Count();
            return page;
        }
    }
}
